import requests
from faker import Faker


class Buildings:
    base_url = "https://smart-test.itheima.net"
    faker = Faker('zh_CN')

    def __init__(self):
        url = self.base_url + "/api/park/login"
        headers = {"Content-Type": "application/json"}
        data = {"username": "admin", "password": "HM_2023_test"}
        response = requests.post(url, headers=headers, json=data)
        assert response.json().get('data').get('token') is not None
        self.token = response.json().get('data').get('token')

    def __str__(self):
        return self.token

    def add(self, json):
        """
        添加楼宇
        :return:
        """
        url = self.base_url + "/api/park/building"
        headers = {"Authorization": self.token}
        # data = {"name": "史迪奇02", "floors": 6, "area": 626, "propertyFeePrice": 626}
        response = requests.post(url, headers=headers, json=json)
        return response.json()

    def check(self, name, page=None, pageSize=None) -> dict:
        """
        page=1&pageSize=10
        查询楼宇
        :return:
        """
        url = self.base_url + "/api/park/building"
        headers = {"Authorization": self.token}
        parmas = {"name": name, "page": page, "pageSize": pageSize}
        # parmas = {"name": name}
        response = requests.get(url, headers=headers, params=parmas)
        return response.json()

    def update(self, id):
        """
        更新楼宇
        必填字段：id
        更新字段：name, floors, area, propertyFeePrice
        :return:
        """
        url = self.base_url + "/api/park/building"
        headers = {"Authorization": self.token}
        json = {"id": id, "name": f"史迪奇的大别野{id}", "floors": 6, "area": 626, "propertyFeePrice": 626}
        response = requests.put(url, headers=headers, json=json)
        print(response.json())
        return response.json()

    def delete(self, id):
        """
        删除楼宇
        :return:
        """
        url = self.base_url + f"/api/park/building/{id}"
        headers = {"Authorization": self.token}
        response = requests.delete(url, headers=headers)
        print(response.json())


if __name__ == '__main__':
    buildings = Buildings()
    for response in buildings.check(name="", page=1, pageSize=1500).get('data').get('rows'):
        # buildings.update(response.get('id'))
        # buildings.update(response.get('id'))
        buildings.delete(response.get('id'))
    #     print(response.get('id'))

    # print(buildings.check(name=""))
